package YunNotes.servlet;

import YunNotes.allnotes.control.NoteAction;
import YunNotes.util.json.Del;
import YunNotes.util.json.GetJson;
import YunNotes.util.json.JsonChange;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

/**
 * @author Hzheng
 * @date 2020-06-03 10:49
 */
@WebServlet(name = "LabelServlet")
public class LabelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());


        //获取id的值
        String s1 = request.getParameter("id");
        int id = Integer.parseInt(s1);

        NoteAction noteAction = new NoteAction();
        JsonChange jsonChang = new JsonChange();
        Del del = new Del();

        try {
            out.println(del.delToJson(jsonChang.ListToJson(noteAction.allLabel(id))));
        } catch (SQLException e) {
            out.println("显示失败！");
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());

        //接收前端数据
        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) {
            // 读取请求数据
            result.append((char) respInt);
            respInt = insr.read();
        }
        System.out.println("前端传递的数据为:" + result);
        insr.close();
        String data = result.toString();

        //获取publicity的值
        GetJson getJson = new GetJson();
        String s1 = getJson.Get(data, "action");
        int action = Integer.parseInt(s1);

        //应用到数据库
        NoteAction noteAction = new NoteAction();

        if (action == 3) {
            String s2 = getJson.Get(data, "num");
            int num = Integer.parseInt(s2);
            String label = getJson.Get(data, "label");
            try {
                noteAction.addLabel(label, num);
                out.println("给num为：" + num + "，添加分类：‘" + label + "’成功！");
            } catch (SQLException e) {
                out.println("添加失败！");
                e.printStackTrace();
            }
        } else if (action == 4) {
            String s2 = getJson.Get(data, "labelNum");
            int labelNum = Integer.parseInt(s2);
            String s3 = getJson.Get(data, "id");
            int id = Integer.parseInt(s3);
            String label = getJson.Get(data, "label");
            try {
                noteAction.addLabelNum(labelNum, label, id);
                out.println("分类：" + label + "设置顺序：" + labelNum + "设置顺序成功！");
            } catch (SQLException e) {
                out.println("设置顺序失败！");
                e.printStackTrace();
            }
        } else {
            out.println("action不是4也不是5。错误！");
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());

        //接收前端数据
        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) {
            // 读取请求数据
            result.append((char) respInt);
            respInt = insr.read();
        }
        System.out.println("前端传递的数据为:" + result);
        insr.close();
        String data = result.toString();

        //获取label和id的值
        GetJson getJson = new GetJson();
        String s1 = getJson.Get(data, "id");
        int id = Integer.parseInt(s1);
        String s2 = getJson.Get(data, "labelNum");
        int labelNum = Integer.parseInt(s2);
        String label = getJson.Get(data, "label");

        NoteAction noteAction = new NoteAction();
        try {
            noteAction.updateLabelNum(labelNum, label, id);
            out.println("分类：‘" + label + "’，现在的顺序为：" + labelNum);
        } catch (SQLException e) {
            e.printStackTrace();
            out.println("修改失败！");
        }

    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());


        //获取label和id和labelNum的值
        String s1 = request.getParameter("id");
        int id = Integer.parseInt(s1);
        String label = request.getParameter("label");

        NoteAction noteAction = new NoteAction();
        try {
            noteAction.delLabel(label, id);
            out.println("删除分类：" + label + "成功！");
        } catch (SQLException e) {
            out.println("删除分类失败");
            e.printStackTrace();
        }

    }
}
